package com.itboye.jinpuyiyuan.activity.me;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cjj.MaterialRefreshLayout;
import com.cjj.MaterialRefreshListener;
import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.adapter.PaymentRecordAdapter;
import com.itboye.jinpuyiyuan.base.BaseActivity;
import com.itboye.jinpuyiyuan.bean.InHosBean;
import com.itboye.jinpuyiyuan.bean.OrderBean;
import com.itboye.jinpuyiyuan.bean.OrderList;
import com.itboye.jinpuyiyuan.presenter.BranchAddressPresenter;
import com.itboye.jinpuyiyuan.utils.sputil.Const;
import com.itboye.jinpuyiyuan.utils.sputil.SPUtils;
import com.itboye.jinpuyiyuan.utils.volley.ResultEntity;
import com.itboye.jinpuyiyuan.widget.loadingutil.MAlert;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

public class InHosRecordActivity extends BaseActivity implements Observer {
    //title
    private ImageView leftImg,rightImg;
    private TextView leftTv,tvTitle;
    private RelativeLayout rlLeft,toobar;

    private MaterialRefreshLayout refresh;
    ListView payment_list;
    PaymentRecordAdapter adapter;
    private List<OrderBean> list;
    private BranchAddressPresenter presenter;
    private String uid;
    private String order_type="2"; //住院

    private String patientId;
    private String inDate;


    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_inhosrecord);

        rightImg.setVisibility(View.GONE);
        leftTv.setVisibility(View.GONE);
        tvTitle.setText("住院缴费记录");

        patientId=getIntent().getStringExtra("patientId");
        uid= SPUtils.get(this,null, Const.UID,"")+"";
        inDate=getIntent().getStringExtra("inDate");


        presenter=new BranchAddressPresenter(this);

        list=new ArrayList<>();
        adapter=new PaymentRecordAdapter(this,order_type,list,R.layout.item_panment_records);
        payment_list.setAdapter(adapter);

        refresh.setLoadMore(false);
        refresh.setMaterialRefreshListener(new MaterialRefreshListener() {
            @Override
            public void onRefresh(final MaterialRefreshLayout materialRefreshLayout) {
                presenter.getYjjRecord(uid,patientId,inDate);
            }

//                @Override
//                public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
//                    page++;
//                    isLoadMore=true;
//                    presenter.getOrderList(uid,order_type,"-1",page+"","10");
//
//                }
        });
        refresh.autoRefresh();

    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){

            case R.id.rlLeft:
            case R.id.leftImg:
                this.finish();
                break;


        }
    }

    @Override
    public void update(Observable o, Object arg) {
        ResultEntity entity = handlerError(arg);
        if (entity!=null) {
            try {
                refresh.finishRefresh();
            } catch (Exception e) {

            }
            if (entity.getEventType() == BranchAddressPresenter.By_Hospitalization_prepayList_success){ //住院订单列表
                if (list.size()>0){
                    list.clear();
                }
                OrderList orderList= (OrderList) entity.getData();
                List<OrderBean> orders = orderList.getPaylist();
                list.addAll(orders);
                adapter.addData(list,order_type);

            }else  if (entity.getEventType() == BranchAddressPresenter.By_Hospitalization_prepayList_fail){
                MAlert.alert(entity.getMsg());
            }
        }
    }
}
